package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.config.CustoRealm;
import com.ktjy.entity.SysUser;
import com.ktjy.entity.vo.SysUserVo;
import com.ktjy.service.ISysRoleService;
import com.ktjy.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @author liuqiaomiao
 * @ClassName SysUserController
 * @since 2024-03-05
 */
@Controller
@RequestMapping("/user")
@CrossOrigin
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;


    /**
     * 获取列表
     */
    @RequestMapping(value = "/getUserList")
    @ResponseBody
    public Object getUserList(@RequestParam(defaultValue = "1") int orderBy, Model model) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();

        if(orderBy==1){//按id升序
            queryWrapper.lambda().orderByAsc(SysUser::getUsrId);
        }else{//降序
            queryWrapper.lambda().orderByDesc(SysUser::getUsrId);
        }
        return   sysUserService.list(queryWrapper);
    }



    /**
     * 跳转新增页面
     */
    @RequestMapping(value = "/add")
    public Object toInsert() {
        return "/user/add";
    }

    /**
     * 增
     */
    @PostMapping(value = "/save")
    public Object insert(SysUser sysUser) {
        if(null!=sysUser.getUsrId()){
            sysUserService.updateById(sysUser);

            //更新用户权限的缓存
           RealmSecurityManager securityManager= (RealmSecurityManager) SecurityUtils.getSecurityManager();
           CustoRealm realm= (CustoRealm) securityManager.getRealms().iterator().next();
            realm.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
        }else{

            sysUser.setUsrPassword(new Md5Hash(sysUser.getUsrPassword(),"czkt").toString());
            sysUserService.save(sysUser);
        }



        return "redirect:/uer/list" ;
    }

    /**
     * 删
     */
    @RequestMapping(value = "/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable(name = "id") Long id) {
        return sysUserService.removeById(id);
    }

    /**
     * 跳转修改页面
     */
//
    @RequestMapping(value = "/edit")
    public Object toUpdate(Integer usrId, Model model) {


        model.addAttribute("roles",sysRoleService.list());
        model.addAttribute("user", sysUserService.getById(usrId));
        return "/user/edit";
    }
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object getDetail(Integer usrId, Model model) {


        model.addAttribute("roles",sysRoleService.list());
        model.addAttribute("user", sysUserService.getById(usrId));
        return sysUserService.getById(usrId);
    }

    @RequestMapping(value = "/aa")
    @ResponseBody
    public Object getDetail(Model model) {


        return sysUserService.getById(1);
    }

//    /**
//     * 改
//     */
//    @PutMapping(value = "/save")
//    @ResponseBody
//    public Object update(SysUser sysUser) {
//        return sysUserService.updateById(sysUser);
//    }


    /**
     * 分页列表
     */
    @RequestMapping(value = "/list")

    public Object getUserPage(Page page, SysUserVo sysUser, Model model) {
        IPage<SysUserVo> pageEntity = sysUserService.pageRecords(page, new QueryWrapper<>(sysUser));
        model.addAttribute("userPager", pageEntity);
        return "/user/list";
    }





}
